/**
 * 
 */
package de.kuaile.wx.httpclient;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HostParams;

import de.kuaile.wx.req.LoginReq;
import de.kuaile.wx.res.LoginRes;

/**
 * @author Csen
 *
 */
public class DefaultWXClient implements IWXClient {

	//帐号密码
	private String username;
	private String pwd;
	
	private HttpClient httpClient;
	
	public DefaultWXClient(String username, String pwd){
		this.username = username;
		this.pwd = DigestUtils.md5Hex(pwd);
		this.httpClient = new HttpClient();
		//设置浏览器头信息
		List<Header> headers = new ArrayList<Header>();
		headers.add(new Header("Host", "mp.weixin.qq.com"));
		headers.add(new Header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0"));
		headers.add(new Header("Accept", "application/json, text/javascript, */*; q=0.01"));
		headers.add(new Header("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"));
		headers.add(new Header("Accept-Encoding", "gzip, deflate"));
		headers.add(new Header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"));
		headers.add(new Header("X-Requested-With", "XMLHttpRequest"));
		//headers.add(new Header("Referer", "https://mp.weixin.qq.com/cgi-bin/loginpage?t=wxm2-login&lang=zh_CN"));
		headers.add(new Header("Connection", "keep-alive"));
		headers.add(new Header("Pragma", "no-cache"));
		headers.add(new Header("Cache-Control", "no-cache"));
		this.httpClient.getParams().setParameter(HostParams.DEFAULT_HEADERS, headers);
		//执行登录操作
		this.doLogin();
	}
	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	
	@SuppressWarnings({ "finally", "unchecked" })
	@Override
	public <RES extends IWXResponse> RES execute(IWXRequest<RES> request) {
		//设置请求URL
		PostMethod method = new PostMethod(request.getWXApiUR());
		//来源地址
		method.addRequestHeader("Referer", request.getReferer());
		//设置请求参数
		method.addParameters(request.getParamArray());
		IWXResponse response = null;
		try {
			//发起请求
			int status = this.httpClient.executeMethod(method);
			//取得响应类型
			Class<RES> clazz = request.getResponseClass();
			response = clazz.newInstance();
			if (status == HttpStatus.SC_OK) {
				response.setBodyString(method.getResponseBodyAsString());
			}else{
				response.setStatus(status);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			return (RES) response;
		}
	}
	
	/**
	 * 方法描述:执行登录操作
	 */
	public void doLogin(){
		LoginReq req = new LoginReq();
		req.setUsername(this.username);
		req.setPwd(this.pwd);
		this.execute(req);
	}
}
